

//
//  SHUserPayPasswordController.m
//  JiFengDaRen
//
//  Created by SH_iOS on 2017/5/31.
//  Copyright © 2017年 SH_iOS. All rights reserved.
//
#import "UserModel.h"
#import "SHUserPayPasswordController.h"

@interface SHUserPayPasswordController ()<UITextFieldDelegate>
@property (nonatomic,strong)    UIView *header;
@property (nonatomic,strong)    UILabel *headerLabel;
@property (nonatomic,strong)    UILabel *bottomLabel;
@property (nonatomic,strong)    UIView *bgView;
@property (nonatomic,strong)    UIView *line;
@property (strong, nonatomic)   UITextField *passWordTf;
@property (strong, nonatomic)   UITextField *confirmPassWordTf;
@property (strong, nonatomic)   UIButton *confirmBtn;

@end

@implementation SHUserPayPasswordController

-(void)viewWillAppear:(BOOL)animated{
    self.tabBarController.tabBar.hidden = YES;
    self.navigationController.navigationBar.hidden = NO;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self configUI];
    
    
}
-(void)configUI{
    
    self.title = @"设置支付密码";
    
    self.view.backgroundColor = KBASEBGCOLOR;
    
    self.header = [[UIView alloc]init];
    self.header.backgroundColor =   [UIColor colorWithHexString:@"ff801a"];
    [self.view addSubview:self.header];
    
    [self.header mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self.view.mas_top).with.offset(0);
        make.left.mas_equalTo(self.view.mas_left).with.offset(0);
        make.right.mas_equalTo(self.view.mas_right).with.offset(0);
        make.height.mas_equalTo(__I6Height(30));
        
        
    }];
    
    
    self.headerLabel = [[UILabel alloc]init];
    self.headerLabel.textColor = [UIColor whiteColor];
    self.headerLabel.font = KSystemFont(__I6Width(13));
    self.headerLabel.text = @"设置支付密码可以保证你的账户安全";
   
    [self.header addSubview:self.headerLabel];
    [self.headerLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.centerY.mas_equalTo(self.header.mas_centerY);
        make.left.mas_equalTo(self.view.mas_left).with.offset(__I6Width(15));
        make.right.mas_equalTo(self.view.mas_right).with.offset(__I6Width(-15));
        make.height.mas_equalTo(__I6Height(13));
        
        
    }];
    
    
    self.bgView = [[UIView alloc]init];
    self.bgView.backgroundColor = [UIColor whiteColor];
    self.bgView.layer.borderWidth = 0.5;
    self.bgView.layer.borderColor = KLINECOLOR.CGColor;
    [self.view addSubview:self.bgView];
    
    [self.bgView mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self.header.mas_bottom).with.offset(0);
        make.left.mas_equalTo(self.view.mas_left).with.offset(0);
        make.right.mas_equalTo(self.view.mas_right).with.offset(0);
        make.height.mas_equalTo(__I6Height(88));
        
        
    }];
    
    
    self.line = [[UIView alloc]init];
    self.line.backgroundColor = KLINECOLOR;
    [self.view addSubview:self.line];
    
    [self.line mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.centerY.mas_equalTo(self.bgView.mas_centerY);
        make.left.mas_equalTo(self.bgView.mas_left).with.offset(__I6Width(15));
        make.right.mas_equalTo(self.bgView.mas_right).with.offset(0);
        make.height.mas_equalTo(0.5);
        
        
    }];
    
    
    self.passWordTf = [[UITextField alloc]init];
    self.passWordTf.delegate = self;
    self.passWordTf.placeholder = @"请输入支付密码";
    self.passWordTf.secureTextEntry = YES;
    self.passWordTf.keyboardType = UIKeyboardTypeNumberPad;
    [self.passWordTf  setValue:[UIColor colorWithHexString:@"#cccccc"] forKeyPath:@"_placeholderLabel.textColor"];
    [self.passWordTf  setValue:KSystemFont(__I6Width(15)) forKeyPath:@"_placeholderLabel.font"];
    self.passWordTf.font = KSystemFont(__I6Width(15));
    self.passWordTf.clearButtonMode =  UITextFieldViewModeAlways;
    [self.bgView addSubview:self.passWordTf];
    
    [self.passWordTf mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.bgView.mas_top).with.offset(0);
        make.left.mas_equalTo(self.bgView.mas_left).with.offset(__I6Width(15));
        make.right.mas_equalTo(self.bgView.mas_right).with.offset(0);
        make.bottom.mas_equalTo(self.line.mas_top).with.offset(0);
    }];
    
    self.confirmPassWordTf = [[UITextField alloc]init];
    self.confirmPassWordTf.delegate = self;
    self.confirmPassWordTf.placeholder = @"确认支付密码";
    self.confirmPassWordTf.secureTextEntry = YES;
    self.confirmPassWordTf.keyboardType = UIKeyboardTypeNumberPad;

    [self.confirmPassWordTf  setValue:[UIColor colorWithHexString:@"#cccccc"] forKeyPath:@"_placeholderLabel.textColor"];
    
    [self.confirmPassWordTf  setValue:KSystemFont(__I6Width(15)) forKeyPath:@"_placeholderLabel.font"];
    self.confirmPassWordTf.font = KSystemFont(__I6Width(15));
    self.confirmPassWordTf.clearButtonMode = UITextFieldViewModeAlways;
    [self.bgView addSubview:self.confirmPassWordTf];
    
    [self.confirmPassWordTf mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.line.mas_bottom).with.offset(0);
        make.left.mas_equalTo(self.bgView.mas_left).with.offset(__I6Width(15));
        make.right.mas_equalTo(self.bgView.mas_right).with.offset(0);
        make.bottom.mas_equalTo(self.bgView.mas_bottom).with.offset(0);
    }];
    
    
    
    self.bottomLabel = [[UILabel alloc]init];
    self.bottomLabel.textColor = [UIColor colorWithHexString:@"#808080"];
    self.bottomLabel.font = KSystemFont(__I6Width(13));
    self.bottomLabel.text = @"请将支付密码设置为6位数字";
    //@"请输入6-20个英文字母/数字/符号,建议组合使用";
    [self.view addSubview:self.bottomLabel];
    [self.bottomLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self.bgView.mas_bottom).with.offset(__I6Height(10));
        make.left.mas_equalTo(self.view.mas_left).with.offset(__I6Width(15));
        make.right.mas_equalTo(self.view.mas_right).with.offset(__I6Width(-15));
        make.height.mas_equalTo(__I6Height(13));
        
        
    }];
    
    
    
    self.confirmBtn = [[UIButton alloc]init];
    self.confirmBtn.titleLabel.font = KSystemFont(__I6Width(17));
    [self.confirmBtn setTitle:@"设置支付密码" forState:UIControlStateNormal];
    
    self.confirmBtn.backgroundColor = [UIColor colorWithHexString:@"2BA2D7"];;
    self.confirmBtn.layer.cornerRadius = __I6Width(5);
    self.confirmBtn.layer.masksToBounds = YES;
    [self.confirmBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.confirmBtn addTarget:self action:@selector(confirmBtn:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.confirmBtn];
    
    [self.confirmBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self.bottomLabel.mas_bottom).with.offset(__I6Height(41));
        make.width.mas_equalTo(__I6Width(326));
        make.height.mas_equalTo(__I6Height(42));
        make.centerX.mas_equalTo(self.view.mas_centerX);
    }];
    
    
    
    
    
    
}
- (void)confirmBtn:(UIButton *)sender {
    [self.view endEditing:YES];
    if (self.passWordTf.text.length == 0) {
        [self.view makeToast:@"请输入支付密码" duration:2 position:@"center"];
        return;
    }
    if (self.confirmPassWordTf.text.length == 0) {
        [self.view makeToast:@"请再次输入支付密码" duration:2 position:@"center"];
        return;
    }
    if (![self.confirmPassWordTf.text isEqualToString:self.passWordTf.text]) {
        [self.view makeToast:@"第二次输入的支付密码与第一次输入的支付密码不一致" duration:2 position:@"center"];
        return;
    }
    if (self.passWordTf.text.length != 6) {
        [self.view makeToast:@"支付密码必须是6位数字" duration:2 position:@"center"];
        return;
    }
    
    NSDictionary *params = @{@"pay_password":_passWordTf.text};
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [MyDataService requestAFURL:KSet_Pay_PassWord httpMethod:@"POST" params:params data:nil complection:^(id result) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        if ([result[@"success"] isEqualToNumber:@(1)]) {
            [self.view makeToast:@"设置支付密码成功" duration:2 position:@"center"];
            [UserModel shareManager].has_pay_password = YES;
            [self.navigationController popViewControllerAnimated:YES];
        }else{
            [self.view makeToast:@"设置支付密码失败" duration:2 position:@"center"];
            
        }
    } failure:^(id erro) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];

    }];
    
}

//参数一：range,要被替换的字符串的range，如果是新键入的那么就没有字符串被替换，range.lenth=0,第二个参数：替换的字符串，即键盘即将键入或者即将粘贴到textfield的string
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string;
{
    if (string.length == 0) {
        return YES;
    }
    if (textField.text.length < 6) {
        return YES;
    }else{
        return NO;
    }
    //第一个参数，被替换字符串的range，第二个参数，即将键入或者粘贴的string，返回的是改变过后的新str，即textfield的新的文本内容
    NSString *checkStr = [textField.text stringByReplacingCharactersInRange:range withString:string];
    //正则表达式
    NSString *regex = @"^\\-?([1-9]\\d*|0)(\\.\\d{0,2})?$";
    return [self isValid:checkStr withRegex:regex];
}
//检测改变过的文本是否匹配正则表达式，如果匹配表示可以键入，否则不能键入
- (BOOL) isValid:(NSString*)checkStr withRegex:(NSString*)regex
{
    NSPredicate *predicte = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex];
    return [predicte evaluateWithObject:checkStr];
}




- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];
    return YES;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
